/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * manage_menu.java
 *
 * Created on Jul 25, 2011, 12:32:58 AM
 */
package introse1;

/**
 *
 * @author Trisha
 */
import javax.swing.table.*;
import javax.swing.table.TableModel.*;
import javax.swing.JTable.*;
import java.sql.ResultSet;

public class manage_menu extends javax.swing.JFrame {

    /** Creates new form manage_menu */
    public manage_menu(managementmodule managementmodule) {
        initComponents();
        ErrorText.setText("");
        DefaultTableModel tmodel = (DefaultTableModel)Menutable.getModel();
        ResultSet res;
        try{
            res = managing.getMenu();
            while(res.next()){
                String[] s1 = {res.getString("name_of_food"), res.getString("price")};
                tmodel.addRow(s1);
            }
        }catch(Exception e){}
        this.managementmodule = managementmodule;
        managementmodule.setVisible(false);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        Menutable = new javax.swing.JTable();
        AddMenuButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        BackButton = new javax.swing.JButton();
        EditMenuButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        DeleteMenu = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        SearchFood = new javax.swing.JTextField();
        ErrorText = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Rockwell", 0, 24));
        jLabel1.setText("Manage Menu");

        Menutable.setAutoCreateRowSorter(true);
        Menutable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Name", "Price"
            }
        ));
        jScrollPane1.setViewportView(Menutable);

        AddMenuButton.setFont(new java.awt.Font("Rockwell", 0, 18));
        AddMenuButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/addmenu.png"))); // NOI18N
        AddMenuButton.setBorder(null);
        AddMenuButton.setBorderPainted(false);
        AddMenuButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddMenuButtonActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Rockwell", 0, 18));
        jLabel2.setText("Edit Menu:");

        BackButton.setFont(new java.awt.Font("Rockwell", 0, 18));
        BackButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/back.png"))); // NOI18N
        BackButton.setBorder(null);
        BackButton.setBorderPainted(false);
        BackButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackButtonActionPerformed(evt);
            }
        });

        EditMenuButton.setBackground(new java.awt.Color(153, 255, 51));
        EditMenuButton.setFont(new java.awt.Font("Rockwell", 0, 18));
        EditMenuButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/editmenu.png"))); // NOI18N
        EditMenuButton.setBorder(null);
        EditMenuButton.setBorderPainted(false);
        EditMenuButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditMenuButtonActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Rockwell", 0, 18));
        jLabel3.setText("Delete Menu:");

        DeleteMenu.setFont(new java.awt.Font("Rockwell", 0, 18));
        DeleteMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/deletemenu.png"))); // NOI18N
        DeleteMenu.setBorder(null);
        DeleteMenu.setBorderPainted(false);
        DeleteMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteMenuActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Rockwell", 0, 18));
        jLabel4.setText("Add a New Menu:");

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/Haj Logo.JPG"))); // NOI18N

        jLabel6.setFont(new java.awt.Font("Rockwell", 0, 18));
        jLabel6.setText("Search:");

        ErrorText.setText("jLabel7");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(BackButton)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel2)
                                        .addComponent(DeleteMenu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel3)
                                        .addComponent(AddMenuButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(EditMenuButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel6)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(SearchFood))
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel5)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(68, 68, 68)
                                .addComponent(ErrorText, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(ErrorText))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel6)
                    .addComponent(SearchFood, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(AddMenuButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(EditMenuButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(DeleteMenu)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 104, Short.MAX_VALUE)
                        .addComponent(BackButton))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BackButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackButtonActionPerformed
        this.setVisible(false);
        managementmodule.setVisible(true);
    }//GEN-LAST:event_BackButtonActionPerformed

    private void AddMenuButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddMenuButtonActionPerformed
        this.setVisible(false);
        add_menu next = new add_menu(this);
        next.setVisible(true);
    }//GEN-LAST:event_AddMenuButtonActionPerformed

    private void EditMenuButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditMenuButtonActionPerformed
        try{
            listahan.setName_of_food((String)Menutable.getModel().getValueAt(Menutable.getSelectedRow(),0));
            this.setVisible(false);
            edit_menu next = new edit_menu(this);
            next.setVisible(true);   
            
        }catch(Exception e){
            if(!SearchFood.getText().isEmpty()){
                listahan.setName_of_food(SearchFood.getText());
                if(managing.checkExists(listahan.getName_of_food()) == 1){
                    System.out.println(managing.checkExists(listahan.getName_of_food()));
                    this.setVisible(false);
                    edit_menu next = new edit_menu(this);
                    next.setVisible(true);
                }
                else{
                    ErrorText.setText("Food Does Not Exist!");
                }
            }
            else{
                ErrorText.setText("Error no food to edit");
            }
        }
    }//GEN-LAST:event_EditMenuButtonActionPerformed

    private void DeleteMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteMenuActionPerformed
        // TODO add your handling code here:
        try{
            listahan.setName_of_food((String)Menutable.getModel().getValueAt(Menutable.getSelectedRow(),0));
            managing.Delete(this);
            DefaultTableModel tmodel = (DefaultTableModel)Menutable.getModel();
            int x = Menutable.getSelectedRow();
            tmodel.removeRow(x);   
            
        }catch(Exception e){
                System.out.println("Error no food to Delete");
        }
    }//GEN-LAST:event_DeleteMenuActionPerformed

    public void setTable(VariableList vars){
        VariableList bago = vars;
        DefaultTableModel tmodel = (DefaultTableModel)Menutable.getModel();
        String s1[] = {vars.getName_of_food(), Float.toString(vars.getPrice_of_food())};
        tmodel.addRow(s1);
    }
    
    public VariableList getListahan(){
        return listahan;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new manage_menu(managementmodule).setVisible(true);
            }
        });
    }
    
    public static MenuManager managing = new MenuManager();
    private static add_menu add_menu;
    private static edit_menu edit_menu;
    private static managementmodule managementmodule;
    public static VariableList listahan = new VariableList();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddMenuButton;
    private javax.swing.JButton BackButton;
    private javax.swing.JButton DeleteMenu;
    private javax.swing.JButton EditMenuButton;
    private javax.swing.JLabel ErrorText;
    private javax.swing.JTable Menutable;
    private javax.swing.JTextField SearchFood;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables
}
